<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/21/021
 * Time: 10:06
 * 定时任务类
 */

namespace Home\Controller;
use Think\Controller;
class TimeController extends Controller
{
    /*
     * 红包返还定时任务， 如果红包在24小时内 没有被领取完 那么就需要把剩余金额返回
     * */
    public function returnHaobao() {
        $hongbaotemp = M('Hongbaotemp');

        $where['add_time'] = ['lt', NOW_TIME - 24*3600];
        $where['is_get'] = 0;
        $where['is_back'] = 0;

        //以为红包剩余的条数不多 可以一次读取所有数据 如果剩余条数过多需要分片读取
        $list = $hongbaotemp->where($where)->select();
        $hongbao = M('Hongbao');
        $register = M('Register');

        foreach ($list as $k => $v) {
            $register_id = $hongbao->where(['id'=>$v['hongbao_id']])->getField('register_id');

            $money = fen_to_yuan($v['money']);

            M()->startTrans();
            $res1 = $register->where(['id'=>$register_id])->setInc('commissions', abs($money));
            $res2 = $hongbaotemp->where(['id'=>$v['id']])->setField('is_back', 1);

            if($res1!==false && $res2!== false) {
                M()->commit();
            } else {
                M()->rollback();
                echo false;
            }
        }

        return ;
    }
}



